﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class SearchRoleCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                control.Value = Request.QueryString["textbox"].ToString();
                //path.Value = Request.QueryString["PageRequest"].ToString();
                BindGrid();
                txtRoleID.Focus();

            }
        }

        #region User Defined Function

        public void BindGrid()
        {
            RoleEntity roleEntity = new RoleEntity();
            UserRole role;
            List<UserRole> roleLst = null;

            try
            {
                if (txtRoleID.Text.Trim() == "")
                {
                    //role = roleEntity.GetRoleDetailsByRoleId(control.Value.Trim(),2);
                    roleLst = roleEntity.GetAllRoles(((User)Session["CurrentUserVO"]).UserType == "S", ((User)Session["CurrentUserVO"]).CustomerID);
                    if (roleLst == null)
                    {
                        lblRolecnt.Text = "No Records Found";
                    }
                    else
                    {
                       // List<UserRole> roles = new List<UserRole>();
                       // roles.Add(role);
                        gvRole.DataSource = roleLst;
                        gvRole.DataBind();
                        lblRolecnt.Text = ((List<UserRole>)gvRole.DataSource).Count.ToString() + " records found.";
                    }
                }
                else
                {
                    role = roleEntity.GetRoleDetailsByRoleId(txtRoleID.Text.Trim(),2);

                    if (role == null)
                    {
                        lblRolecnt.Text = "No Records Found";
                    }
                    else
                    {
                        List<UserRole> roles = new List<UserRole>();
                        roles.Add(role);
                        gvRole.DataSource = roles;
                        gvRole.DataBind();
                        lblRolecnt.Text = ((List<UserRole>)gvRole.DataSource).Count.ToString() + " records found.";
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        //protected void gvRole_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    gvRole.PageIndex = e.NewPageIndex;
        //    BindGrid();
        //}

        protected void gvRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            string roleID = ((LinkButton)gvRole.SelectedRow.FindControl("lbtnRoleID")).Text;
            string strScript = "<script>";
            strScript += "window.opener.document.all('ctl00$BodyHolder$txtRoleID').value = '" + roleID + "';";
            strScript += "self.close();</script>";
            Response.Write(strScript);
        }

        protected void gvRole_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }


    }
}